package com.example.snashuitraverl.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.snashuitraverl.demos.common.ErrorCode;
import com.example.snashuitraverl.demos.domain.Ads;
import com.example.snashuitraverl.demos.domain.DTO.AdsDto;
import com.example.snashuitraverl.demos.domain.User;
import com.example.snashuitraverl.demos.exception.BusinessException;
import com.example.snashuitraverl.demos.exception.ThrowUtils;
import com.example.snashuitraverl.demos.mapper.UserMapper;
import com.example.snashuitraverl.demos.service.AdsService;
import com.example.snashuitraverl.demos.mapper.AdsMapper;
import com.example.snashuitraverl.demos.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.WatchEvent;
import java.util.List;

/**
 * @author 32551
 * @description 针对表【ads(广告)】的数据库操作Service实现
 * @createDate 2024-06-23 18:59:56
 */
@Service
public class AdsServiceImpl extends ServiceImpl<AdsMapper, Ads>
        implements AdsService {

    @Resource
    private UserService userService;

    @Resource
    private AdsMapper adsMapper;


    @Override
    public int addAds(Ads ads, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还未登录");
        }
        if (loginUser.getRole() != 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还不是管理员没有权限");
        }
        int insert = adsMapper.insert(ads);
        if (insert != 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return insert;
    }

    @Override
    public List<Ads> selectAll(HttpServletRequest request) {
//        User loginUser = userService.getLoginUser(request);
//        if (loginUser == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还未登录");
//        }
//        if (loginUser.getRole() != 1) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还不是管理员没有权限");
//        }
        List<Ads> ads = adsMapper.selectList(new QueryWrapper<Ads>());
        if (ads == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ads;
    }

    public User loginUsers(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还未登录");
        }
        if (loginUser.getRole() != 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你还不是管理员没有权限");
        }
        return loginUser;
    }


    @Override
    public Ads selectByID(int ID, HttpServletRequest request) {
        loginUsers(request);
        Ads adsID = adsMapper.selectOne(new QueryWrapper<Ads>().eq("adsID", ID));
        return adsID;
    }

    @Override
    public int updateID(Ads ads, HttpServletRequest request) {
        Integer adsID = ads.getAdsID();
        String adsAvatural = ads.getAdsAvatural();
        String adsLink = ads.getAdsLink();
        loginUsers(request);
        int update = adsMapper.update(null, new LambdaUpdateWrapper<Ads>()
                .eq(Ads::getAdsID, adsID)
                .set(Ads::getAdsAvatural, adsAvatural)
                .set(Ads::getAdsLink, adsLink)
        );
        if (update == -1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新错误");
        }
        return update;
    }

    @Override
    public Page<Ads> selectPage(AdsDto adsDto, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Ads ads = new Ads();
        BeanUtils.copyProperties(ads, adsDto);
        Page<Ads> adsPage = new Page<>(adsDto.getPageNum(), adsDto.getPageSize());
        QueryWrapper<Ads> userQueryWrapper = new QueryWrapper<>(ads);
        Page<Ads> userPage = adsMapper.selectPage(adsPage, userQueryWrapper);
        return userPage;
    }
}




